//#ifdef OLD_HEADER_FILENAME
//#include <iostream.h>
//#else
//#include <iostream>
//#endif
//#include <string>
//
//#ifndef H5_NO_NAMESPACE
//#ifndef H5_NO_STD
//using std::cout;
//using std::endl;
//#endif  // H5_NO_STD
//#endif
//
//#include "H5Cpp.h"
//
//#ifndef H5_NO_NAMESPACE
//using namespace H5;
//#endif
//
//#include "HDFTest.h"
//#include <string>
//#include <vector>
//using namespace std;
#include "HDF5DataSetObject.h"
//#include "matrix.h"
#include "solver.h"
const H5std_string DATASET_NAME( "/IntArray" );

#pragma region create_file

const int       NX = 5;                    // dataset dimensions
const int       NY = 6;
const int       RANK = 1;
const int LENGTH=10;
const int ARRAY_RANK=1;
const int ARRAY_DIM=3;

class s1_t:CompType
{
public:
	int a;
	float b[ARRAY_DIM];
};



//void Comp::setType()
//{
//	hsize_t dim[]={LENGTH};
//	hsize_t array_dim[]={ARRAY_DIM};
//
//	ArrayType array_tid(PredType::NATIVE_FLOAT,ARRAY_RANK,array_dim);
//	dType=new CompType(sizeof(s1_t));
//	dType->insertMember("a_name",HOFFSET(s1_t,a),PredType::NATIVE_INT);
//	dType->insertMember("b_name",HOFFSET(s1_t,b),array_tid);
//}
// Operator function
extern "C" herr_t file_info(hid_t loc_id, const char *name, const H5L_info_t *linfo,
							void *opdata);
extern "C" herr_t data_info(hid_t loc_id, const char *name, const H5L_info_t *linfo,
							void *opdata);
int main (void)
{

	const char* FILE_NAME("s1.h5");

	string ttstr("a.ss.tt");
	string subst(ttstr.substr(ttstr.find_last_of('.'),ttstr.size()));
	H5File* itfile=new H5File("C:\\test.h5",H5F_ACC_RDONLY);
	int num=itfile->getObjCount();
	string n(itfile->getObjnameByIdx(0));
	Group gg(itfile->openGroup("/Main/members/MyBVHobject/members/Data/members/Hip/members/CHANNELS"));
	int num2=gg.getNumObjs();
	n=gg.getObjnameByIdx(0);
	DataSet ds(gg.openDataSet("Data"));

	double out[101];
	for (int i=0;i<101;i++)
	{
		out[i]=0;
	}
	H5T_class_t type_class = ds.getTypeClass();
	ds.read(out,PredType::NATIVE_DOUBLE,ds.getSpace(),ds.getSpace());

	DataType dt=ds.getDataType();
	DataType tdt(PredType::NATIVE_DOUBLE);
	if (dt==PredType::NATIVE_INT)
	{
		return 1;
	}
	int num1=gg.getNumAttrs();
	unsigned int idx1=1;
	Attribute attr1=gg.openAttribute("Name");

	size_t st=attr1.getStorageSize();
	StrType tid1(PredType::C_S1,st);
	char s[30];
	string buf("");
	attr1.read(tid1,buf);
	H5G_obj_t tt=gg.getObjTypeByIdx(0);
	cout << endl << "Iterating over elements in the file" << endl;
	herr_t idx = H5Literate(itfile->getId(), H5_INDEX_NAME, H5_ITER_INC, NULL, file_info, NULL);
	cout << endl;

	//string test="Four";

	//hid_t		native_type;       /* Datatype ID */
	//hsize_t		dims1[1];
	//dims1[0]=test.length();

	//H5File* file1=new H5File( FILE_NAME, H5F_ACC_TRUNC);
	///* Create dataspace for datasets */
	//DataSpace sid1(1, dims1);

	///* Create a datatype to refer to */
	//StrType tid1(PredType::C_S1);

	//if(H5T_STRING!=H5Tget_class(tid1.getId()) || !H5Tis_variable_str(tid1.getId()))
	//	cerr << "this is not a variable length string type!!!" << endl;

	////file1->createGroup("StringTest");
	///* Create a dataset */
	//DataSet dataset = file1->createDataSet("StringTest", tid1, sid1);

	/////* Write dataset to disk */
	//dataset.write(test, tid1);

	/////* Close Dataset */
	//dataset.close();
	//delete file1;

	//H5File* file=new H5File( FILE_NAME, H5F_ACC_TRUNC);
	//file->createGroup("test");
	//delete file;
	H5File* file=new H5File( FILE_NAME, H5F_ACC_TRUNC);
	//file->unlink("test");
	hsize_t dimsf[1];
	dimsf[0]=1;
	DataSpace dds(0,0);
	IntType ddt(PredType::NATIVE_INT);
	DataSet ddset=file->createDataSet("TEST",ddt,dds);
	int data=1;
	ddset.write(&data,PredType::NATIVE_INT);

	ddset.close();
	delete file;

	Group *g=new Group(file->createGroup("test"));
	g->createGroup("test");
	g->close();
	delete file;
	int* dd=new int[6];
	dd[0]=1;
	dd[1]=4;
	dd[2]=2;
	dd[3]=5;
	dd[4]=3;
	dd[5]=6;
	int* dd2=new int[6];
	int r=0,c=0;
	for (int j=0; j<3; j++)
		for (int i=0; i<2; i++)
			dd2[j + i*3] = dd[i + j*2];


	for (int i=0;i<6;i++)
	{
		cout<<dd[i];
	}
	cout<<endl;
	for (int i=0;i<6;i++)
	{
		cout<<dd2[i];
	}
	delete[]dd;
	delete[]dd2;


	CSolver* cs=new CSolver();
	//cs->read("cs.h5","/","CSolver");
	//if(!cs->append("cs.h5","/","CSolver"))
	//	cout<<cs->GetErrMsg().c_str()<<endl;
	cs->write("cs.h5","/","CSolver");
	//H5File* h5_file=new H5File( "s2.h5", H5F_ACC_RDONLY);

	//DataSet dataset=h5_file->openDataSet(DATASET_NAME);
	//DataSpace ds=dataset.getSpace();
	//int rank=ds.getSimpleExtentNdims();
	//hsize_t* dims_out=new hsize_t[rank];
	//int ndims=ds.getSimpleExtentDims(dims_out);
	//int tt=dataset.getDataType().getSize();
	//double dd[12];

	//dataset.read(dd,PredType::NATIVE_DOUBLE,ds,ds);
	//Attribute dimAttr=dataset.openAttribute("Data size");
	//int ss;
	//dimAttr.read(PredType::NATIVE_INT,&ss);
	//Matrix m(2,3,1.0);
	//m.printMatrix();
	//Matrix n=m.read("s2.h5",DATASET_NAME);
	//m.write("s2.h5",DATASET_NAME);
	//int tt;
	//cin>>tt;
	//s1_t* s1=new s1_t[tt];
	s1_t s1[LENGTH];
	//vector<s1_t>s1(LENGTH);
	//hid_t s1_tid;
	//hid_t array_tid;
	//hid_t dataset,space;
	//herr_t status;
	hsize_t dim[]={LENGTH};
	hsize_t array_dim[]={ARRAY_DIM};
	int i,j;

	for(i=0;i<LENGTH;i++){
		s1[i].a=i;
		for(j=0;j<ARRAY_DIM;j++){
			s1[i].b[j]=i+j;
		}
	}
	DataSpace dataspace(RANK,dim);
	try
	{
		Exception::dontPrint();
		H5File* file=new H5File( FILE_NAME, H5F_ACC_TRUNC);

		H5std_string fname=file->getFileName();
	ArrayType array_tid(PredType::NATIVE_FLOAT,ARRAY_RANK,array_dim);
	CompType s1_tid(sizeof(s1_t));
	DataType& dType=s1_tid;
	s1_tid.insertMember("a_name",HOFFSET(s1_t,a),PredType::NATIVE_INT);
	s1_tid.insertMember("b_name",HOFFSET(s1_t,b),array_tid);

	//CHDF5* hdf=new CHDF5(RANK,dim,s1_tid);
	//hdf->write(FILE_NAME,"/IntArray/t",s1);
	Group group(file->createGroup("/IntArray"));
	//DataSet *dataset=new DataSet(file->createDataSet("/IntArray/t",dType,dataspace));
	//dataset->write(s1,dType);
	//H5T_class_t type_class=dataset->getTypeClass();
	//CompType compType=dataset->getCompType();
	//size_t size=compType.getSize();
	//DataSpace ds=dataset->getSpace();
	//int rank=ds.getSimpleExtentNdims();
	//hsize_t* dims_out=new hsize_t[rank];
	//int ndims=ds.getSimpleExtentDims(dims_out);
	//
	////DataSet *dataset2=new DataSet(file->createDataSet(DATASET_NAME,s1_tid,dataspace));
	////dataset2->write(s1,s1_tid);
	//delete dataset;
	//delete file;

	////delete[] s1;
	//delete[] dims_out;
	}
	catch(FileIException error)
	{
		H5std_string errMsg=error.getDetailMsg();
	}
	// catch failure caused by the DataSet operations
	catch( DataSetIException error )
	{
		H5std_string errMsg=error.getDetailMsg();
	}

	// catch failure caused by the DataSpace operations
	catch( DataSpaceIException error )
	{
		H5std_string errMsg=error.getDetailMsg();
	}

	// catch failure caused by the DataSpace operations
	catch( DataTypeIException error )
	{
		H5std_string errMsg=error.getDetailMsg();
	}
}
herr_t
file_info(hid_t loc_id, const char *name, const H5L_info_t *linfo, void *opdata)
{
	hid_t group;
   /*
	*
	* Open the group using its name.
	*/
	string s(name);
	s.append("/members");
	group = H5Gopen2(loc_id, name, H5P_DEFAULT);
	/*
	* Display group name.
	*/
	cout << "Name : " << name << endl;
	
	//if (s=="matrix")
	//{

	//	hid_t dataset=H5Dopen2(group,"/matrix/data",H5P_DEFAULT);
	//	if( dataset < 0) printf(" Dataset is not found. \n");
	//	printf("\"/Data_new/Compressed_Data\" dataset is open \n");
	//	DataSet dataSet(dataset);
	//	/*
	//	* Close the dataset.
	//	*/
	//	H5Dclose(dataset);
	//}

	//H5Gclose(group);
	return 0;
}

herr_t
data_info(hid_t loc_id, const char *name, const H5L_info_t *linfo, void *opdata)
{
	hid_t data;

	/*
	* Open the group using its name.
	*/
	data = H5Dopen2(loc_id, name, H5P_DEFAULT);
	/*
	* Display group name.
	*/
	cout << "Name : " << name << endl;

	H5Dclose(data);
	return 0;
}